import java.util.Arrays;

public class MyPriorityQueue {
    int[] arr = { 27,15,19,18,28,34,65,49,25,37 };
    int useSize = 10;
    public void downSort(int p,int len){
        int child = p*2+1;
        while (child < len){
            if (child+1 < len && arr[child] < arr[child+1]){
               child++;
            }
            if (arr[child] > arr[p]){
                int ret = arr[child];
                arr[child] = arr[p];
                arr[p] = ret;
                p = child;
                child = child*2+1;
            }else {
                break;
            }
        }
    }
    public void maxSort(){
        int len = arr.length;
        for (int i = (len-2)/2; i >=0 ; i--) {
            downSort(i,len);
        }
    }
    public boolean isFull(){
        return arr.length == useSize;
    }
    public void shiftUP(int child){
        int parent = (child-1)/2;
        while (child > 0){
            if (arr[child] > arr[parent]){
                int ret = arr[child];
                arr[child] = arr[parent];
                arr[parent] = ret;
                child = parent;
                parent = (parent-1)/2;
            }else {
                break;
            }
        }
    }
    public void add(int val){
        if (isFull()) {
            arr = Arrays.copyOf(arr,arr.length*2);
        }
        arr[useSize] = val;
        shiftUP(useSize);
        useSize++;
    }
    public void delete(){
        if (useSize == 0){
            System.out.println("优先级队列为空");
        }
        arr[0] = arr[useSize-1];
        useSize--;
        downSort(0,useSize);
    }
}
